﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MethodWorx.CMS.UI.Areas.Application.Models;
using MethodWorx.CMS.UI.Application;
using MethodWorx.CMS.Services;
using MethodWorx.CMS.TemplateLanguage;
using MethodWorx.CMS.TemplateLanguage.Properties;

namespace MethodWorx.CMS.UI.Areas.Application.Controllers
{
    public class TemplatePropertyController : ApplicationController
    {
        private IModelFactory<TemplateModel> modelFactory;
        private SchemaService service;

        public TemplatePropertyController(IModelFactory<TemplateModel> modelFactory, SchemaService service)
        {
            this.modelFactory = modelFactory;
            this.service = service;
        }

        [HttpGet]
        public JsonResult GetChildContentTemplates(Guid id)
        {
            var model = this.modelFactory.CreateModel(this.CurrentUser);
            model.Initialize(id);

            List<object> templates = new List<object>();
            foreach (var template in model.Templates)
            {
                templates.AddRange(model.PageTemplate.ContentTemplateConstraints.Select(c => new { Related = true, Name = c.RelatedTemplate.Name }));
            }

            foreach (var template in this.service.GetAllContentTemplates())
            {
                templates.Add(new { Related = false, Name = template.Name });
            }

            return Json(templates.Distinct().ToArray(), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetContentTemplateProperties(Guid id, int index, string name)
        {
            var model = this.modelFactory.CreateModel(this.CurrentUser);
            model.Initialize(id);

            var parser = model.GetTemplateParser().GetParser(index);
            List<object> properties = new List<object>();
            if (parser != null)
            {
                //
                //  ok, get the parser property
                var attr = ParserPropertyHelper.GetPropertyAttribute<ContentTemplatePropertyPropertyAttribute>(parser.GetType(), name);
                if (attr != null)
                {
                    //  ok, this is the one
                    //  get the template name
                    var tp = attr.TemplateProperty;
                    //  ok, lets do that
                    string templateName = parser.GetType().GetProperty(tp).GetValue(parser, null) as string;
                    if (!string.IsNullOrEmpty(templateName))
                    {
                        //  
                        //  ok, we have our template
                        foreach (var template in this.service.GetAllContentTemplates())
                        {
                            if (template.Name.ToLower() == templateName.ToLower())
                            {
                                //  this is the one we want
                                //  get all the properties
                                foreach (var property in template.Properties)
                                {
                                    properties.Add(new { Name = property.Name });
                                }
                            }
                        }
                    }
                }
            }

            return Json(properties.Distinct().ToArray(), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetChildPageTemplates(Guid id)
        {
            var model = this.modelFactory.CreateModel(this.CurrentUser);
            model.Initialize(id);

            List<object> templates = new List<object>();
            foreach (var template in model.Templates)
            {
                templates.AddRange(model.PageTemplate.PageTemplateConstraints.Select(c => new { Related = true, Name = c.RelatedTemplate.Name }));
            }

            foreach (var template in this.service.GetAllPageTemplates().Where(p => p.Abstract == false))
            {
                templates.Add(new { Related = false, Name = template.Name });
            }

            return Json(templates.Distinct().ToArray(), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult GetPageTemplateProperties(Guid id, int index, string name)
        {
            var model = this.modelFactory.CreateModel(this.CurrentUser);
            model.Initialize(id);

            var parser = model.GetTemplateParser().GetParser(index);
            List<object> properties = new List<object>();
            if (parser != null)
            {
                //
                //  ok, get the parser property
                var attr = ParserPropertyHelper.GetPropertyAttribute<PageTemplatePropertyPropertyAttribute>(parser.GetType(), name);
                if (attr != null)
                {
                    //  ok, this is the one
                    //  get the template name
                    var tp = attr.TemplateProperty;
                    //  ok, lets do that
                    string templateName = parser.GetType().GetProperty(tp).GetValue(parser, null) as string;
                    if (!string.IsNullOrEmpty(templateName))
                    {
                        //  
                        //  ok, we have our template
                        foreach (var template in this.service.GetAllPageTemplates().Where(p => !p.Abstract))
                        {
                            if (template.Name.ToLower() == templateName.ToLower())
                            {
                                //  this is the one we want
                                //  get all the properties
                                foreach (var property in template.Properties)
                                {
                                    properties.Add(new { Name = property.Name });
                                }
                            }
                        }
                    }
                }
            }

            return Json(properties.Distinct().ToArray(), JsonRequestBehavior.AllowGet);
        }


    }
}
